package com.sharainy.service.impl;


import com.sharainy.dao.AccountMapper;
import com.sharainy.dao.MerchantMapper;
import com.sharainy.dao.MeterMapper;
import com.sharainy.dao.SettlementMapper;
import com.sharainy.pojo.Account;
import com.sharainy.pojo.Merchant;
import com.sharainy.pojo.Meter;
import com.sharainy.pojo.SingleSettlement;
import com.sharainy.service.MerchantService;
import com.sharainy.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class MerchantServiceImpl implements MerchantService {
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private MeterMapper meterMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SettlementMapper settlementMapper;


    @Override
    public void addMerchant(Merchant merchant) {
        merchantMapper.addMerchant(merchant);
    }

    @Override
    public List<Merchant> selectAllMerchant(String companyId) {
        return merchantMapper.selectAllMerchant(companyId);
    }

    @Override
    public Merchant selectMerchantById(String id) {
        return merchantMapper.selectMerchantById(id);
    }

    @Override
    @Transactional
    public void updateMerchant(Merchant merchant) {
        String[] meterArrA = merchant.getMeters().split(",");
        Merchant selectMerchant = merchantMapper.selectMerchantById(merchant.getId()+"");
        String[] meterArrB = selectMerchant.getMeters().split(",");
        List<String> listA = Arrays.asList(meterArrA);
        List<String> listB = Arrays.asList(meterArrB);
//用来装差集
        List<String> resultA = new ArrayList<String>();//A有B没有 state-->1
        List<String> resultB = new ArrayList<String>();//B有A没有 state-->0
        for(String a : meterArrA){
            //判断是否包含
            if(!listA.contains(a)){
                resultA.add(a);
            }
        }
        for(String b : meterArrB){
            //判断是否包含
            if(!listB.contains(b)){
                resultB.add(b);
            }
        }
        for (String A:resultA) {
            meterMapper.setMeterStateToU(A);
        }
        for (String B:resultB) {
            meterMapper.setMeterStateToN(B);
        }
        merchantMapper.updateMerchant(merchant);
    }

    @Override
    @Transactional
    public void updateStates(String[] ids) {
        Account account = new Account();
        Merchant merchant = merchantMapper.selectMerchantById(ids[0]);
        String[] meters = merchant.getMeters().split(",");
        for (int i=0;i<meters.length;i++){
            Meter meter = meterMapper.selectMeterById(meters[i]);
            Account account1 = new Account();
            if (meter.getProtocoltype()==1){
                account1.setName("电子账户");
                account1.setPowertype(0);
            }else if (meter.getProtocoltype()==2){
                account1.setName("水子账户");
                account1.setPowertype(1);
            }else if (meter.getProtocoltype()==3){
                account1.setName("气子账户");
                account1.setPowertype(2);
            }
            account1.setBeforereading(Constant.before_reading);
            account1.setCollectors(meters[i]);
            account1.setMerchantid(merchant.getId());
            account1.setNowreading(Constant.before_reading);
            account1.setTime(System.currentTimeMillis()/1000);
            accountMapper.addAccount(account1);
            SingleSettlement singleSettlement = new SingleSettlement();
            singleSettlement.setAccountid(account1.getId());
            singleSettlement.setMerchantid(account1.getMerchantid());
            singleSettlement.setNowreading(account1.getNowreading());
            singleSettlement.setCode(account1.getCollectors());
            singleSettlement.setTime(account1.getTime());
            singleSettlement.setBeforereading(account1.getBeforereading());
            settlementMapper.addSingleSettlement(singleSettlement);
        }
        for (String id:ids) {
            merchantMapper.updateState(id);
        }


    }

    @Override
    public String selectCompanyName(int id) {
        return merchantMapper.selectCompanyName(id);
    }

    @Override
    public List<Merchant> selectOppenMerchant(String companyId) {
        return merchantMapper.selectOppenMerchant(companyId);
    }

    @Override
    public void pinHouseholds(String id) {
        merchantMapper.pinHouseholds(id);
    }

    @Override
    public void settlementState(String id) {
        merchantMapper.settlementState(id);
    }

    @Override
    public List<Merchant> selectSettleMerchant(String companyId) {
        return merchantMapper.selectSettleMerchant(companyId);
    }

    @Override
    public void modifyBalance(Map<String, String> map) {
        merchantMapper.modifyBalance(map);
    }

}
